package com.api.shopapi.mapper;

import com.api.shopapi.entity.DeviceLXXueYaJi;
import com.api.shopapi.entity.dto.DeviceLXXueYaJiDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Mapper
@Repository
public interface DeviceLXXueYaJiMapper {

    @Insert("insert into device_lxxueyaji " +
            "(" +
            "DeviceLXXueYaJiCode, " +
            "UserCode, " +
            "SN," +
            "DeviceId," +
            "BindTime " +
            ") " +
            "VALUES " +
            "(" +
            "#{deviceLXXueYaJi.DeviceLXXueYaJiCode} ," +
            "#{deviceLXXueYaJi.UserCode} ," +
            "#{deviceLXXueYaJi.SN}, " +
            "#{deviceLXXueYaJi.DeviceId}, " +
            "#{deviceLXXueYaJi.BindTime} " +
            ")")
    int insert(@Param("deviceLXXueYaJi") DeviceLXXueYaJi deviceLXXueYaJi);


    @Update("update device_lxxueyaji " +
            "set " +
            "UserCode=#{deviceLXXueYaJi.UserCode} ," +
            "SN=#{deviceLXXueYaJi.SN}, " +
            "DeviceId=#{deviceLXXueYaJi.DeviceId}, " +
            "BindTime=#{deviceLXXueYaJi.BindTime} " +
            "where " +
            "DeviceLXXueYaJiCode=#{deviceLXXueYaJi.DeviceLXXueYaJiCode}   ")
    int update(@Param("deviceLXXueYaJi") DeviceLXXueYaJi deviceLXXueYaJi);

    @Select("select  * from  device_lxxueyaji where UserCode=#{userCode} limit 1 ")
    DeviceLXXueYaJi getByUserCode(@Param("userCode") String userCode);

    @Select("select * from device_lxxueyaji where DeviceLXXueYaJiCode=#{deviceLXXueYaJiCode}  limit 1  ")
    DeviceLXXueYaJi getByDeviceLXXueYaJiCode(@Param("deviceLXXueYaJiCode") String deviceLXXueYaJiCode);

    @Select("select  * from device_lxxueyaji where SN=#{sn}  limit 1 ")
    DeviceLXXueYaJi getBySN(@Param("sn") String sn);

    @Select("<script>" +
            "select dl.*,u.Name as UserName,u.Phone  from device_lxxueyaji dl left join user u on dl.UserCode = u.UserCode " +
            "left join user_device ud on ud.UserCode = dl.UserCode " +
            "where dl.SN is not null " +
            "and ud.LXXueYaJi = 1 " +
            "<if test ='phone !=null and phone!=\"\" '>" +
            " and u.Phone like concat('%',#{phone},'%') " +
            "</if>" +
            "<if test ='sn !=null and sn!=\"\" '>" +
            " and dl.SN =#{sn} " +
            "</if>" +
            "</script>")
    List<DeviceLXXueYaJiDTO> getPage(@Param("phone") String phone, @Param("sn") String sn);




}
